home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
a_utils
/
perl
/
prlbkxmp.lha
/
ch6
/
tgrep
< prev
next >
Wrap
Text File
|
1991-01-08
|
824b
|
51 lines
#!/usr/bin/perl
# Usage: tgrep [-l] pattern [files]
# Handle
if ($ARGV[0] eq '-l') {
shift;
$action = <<'EOF';
print $file,"\n";
next FILE;
EOF
}
else {
$action = <<'EOF';
print $file,":\t", $_;
EOF
}
# Get pattern and protect the delimiter we'll use.
$pat = shift;
$pat =~ s/!/\\!/g;
# Generate the program.
$prog = <<EOF;
FILE: foreach \$file (\@ARGV) {
open(FILE,\$file) || do {
print STDERR "Can't open \$file: \$!\\n";
next;
};
next if -d FILE; # ignore directories
next if -B FILE; # ignore binary files
while (<FILE>) {
if (m!$pat!) {
$action
}
}
}
EOF
# We often put in lines like this while developing scripts, so we
# can see what program the program is writing.
print $prog if $debugging;
# And finally, do it.
eval $prog;
die $@ if $@;